<template>
	<view class="address-content">
		<view class="address-list" v-if="siteList.length">
			<view class="address-item" v-for="(item, index) in siteList" :key="index" @click="chose(item.address_id)">
				<view class="address-item-info">
					<view class="top">
						<view class="name">{{ item.consignee }}</view>
						<view class="phone">{{ item.mobile }}</view>
						<view class="tag">
							<text class="black" v-if="item.default_id == 2">默认</text>
						</view>
					</view>
					<view class="bottom">
						{{item.province}}{{item.city}}{{item.district}} {{item.address}}
					</view>
				</view>
				<view class="icon-img" @click.stop="editAddress(item.address_id)">
					<image src="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/static/images/other/2020-08-14/20200814/b350ba7854abc7c9c6feaf38a0fcd015.png" mode=""></image>
				</view>
			</view>
		</view>
		<pt-nothing
			text="暂无收货地址" 
			:fixed="true" 
			v-if="noData" 
			icon="https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/images/shop/icons/nothing.png?v=1">
		</pt-nothing>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
		<pt-loading></pt-loading>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isPreview: false,
				noData:false,
				siteList: []
			};
		},
		onLoad(options) {
			if(options.type==='preview'){
				this.isPreview = true
			}
		},
		onShow() {
			this.getData();
		},
		methods: {
			getData() {
				let data = {
					page: 0,
					limit: 50,
				}
				this.$u.apiWei.addressList(data).then(res => {
					this.siteList = res.datas
					if(res.datas.length === 0){
						this.noData = true;		
					}
				})
			},
			toAddSite() {
				this.noData = false;
				uni.navigateTo({
					url: '/my/address-list/add-address/add-address?type=0'
				});
			},
			editAddress(address_id) {
				this.noData = false;
				uni.navigateTo({
					animationDuration: 500,
					url: '/my/address-list/add-address/add-address?type=1&id=' + address_id
				})
			},
			// 选择地址
			chose(address_id){
				if(!this.isPreview){
					return false
				}
				var pages = getCurrentPages();
				var prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.$vm.choseAddressFun({address_id: address_id});
				uni.navigateBack()
			}
		}
	};
</script>

<style lang="scss" scoped>
	.address-content {
		

		.address-list {
			padding: 0 30rpx;
			margin-bottom: 150rpx;

			.address-item {
				padding: 40rpx 30rpx;
				display: flex;
				border-bottom: 2rpx solid #F4F4F4;

				.address-item-info {
					flex: 1;

					.top {
						display: flex;
						font-weight: bold;
						font-size: 34rpx;

						.phone {
							margin-left: 60rpx;
						}

						.tag {
							display: flex;
							font-weight: normal;
							align-items: center;

							text {
								display: block;
								width: 60rpx;
								height: 34rpx;
								line-height: 34rpx;
								color: #ffffff;
								font-size: 20rpx;
								border-radius: 6rpx;
								text-align: center;
								margin-left: 20rpx;
								background-color: rgb(49, 145, 253);
							}

							.black {
								background-color: #141414;
							}
						}
					}

					.bottom {
						display: flex;
						margin-top: 20rpx;
						font-size: 28rpx;
						justify-content: space-between;
						color: #999999;
					}
				}

				.icon-img {
					display: flex;
					width: 25rpx;
					justify-content: center;
					align-items: center;

					image {
						display: block;
						width: 25rpx;
						height: 24rpx;
					}
				}
			}
		}

		.addSite {
			display: flex;
			justify-content: space-around;
			width: 600rpx;
			line-height: 90rpx;
			position: fixed;
			bottom: 30rpx;
			left: 80rpx;
			background: #131313;
			border-radius: 8rpx;
			font-size: 30rpx;
			z-index: 10;
			.add {
				display: flex;
				align-items: center;
				color: #ffffff;

				.icon {
					margin-right: 10rpx;
				}
			}
		}
	}
</style>
